package com.nstar.pum.view
{
	import com.nstar.base.event.RequestEvent;
	import com.nstar.base.util.EffectUtils;
	import com.nstar.base.util.SystemUtils;
	
	import flash.events.MouseEvent;
	
	import mx.containers.Canvas;
	import mx.controls.Image;
	import mx.controls.Label;
	import mx.events.FlexEvent;

	public class Money extends Canvas
	{
		public static const REMOVE_MONEY:String = "removeMoney";
		public static const CLICKED_MONEY:String = "clickedMoney";
		public static const CURRENT_MONEY:String = "currentMoney";
		public static const ALLOW_CLICKED:String = "allowClicked";
		public static const MIN_SPEED:int = 10;
		public static const MAX_SPEED:int = 40;
		
		
		public var value:Number;
		public var values:Array;
		public var duration:int;
		public var url:String;
		
		public var moneys:Array;
		
		private var title:Label;
		private var allowedClick:Boolean = true;
		private var clicked:Boolean = false;
		
		public function Money(value:Number,values:Array,duration:int,w:Number,h:Number,url:String)
		{
			super();
			
			this.value = value;
			this.values = values;
			this.duration = duration;
			this.width = w;
			this.height = h;
			this.url = url;
			
			//显示手形
			this.buttonMode = true;
			this.mouseChildren = false;
			
			this.addEventListener(FlexEvent.CREATION_COMPLETE,init);
			this.addEventListener(ALLOW_CLICKED,allowClickHandler);
		}
		
		private function allowClickHandler(event:RequestEvent):void{
			allowedClick = event.request as Boolean;
		}
		
		override protected function createChildren():void{
			super.createChildren();
			
			var img:Image = new Image();
			img.setStyle("verticalCenter",0);
			img.setStyle("horizontalCenter",0);
			img.percentWidth = 100;
			img.percentHeight = 100;
			img.maintainAspectRatio = false;
			img.source = url;
			this.addChild(img);
		}
		
		private function mouseHandler(event:MouseEvent):void{
			switch(event.type){
				case MouseEvent.MOUSE_DOWN:
					this.startDrag();
					break;
				case MouseEvent.MOUSE_UP:
					this.stopDrag();
					break;
			}
		}
		
		private function init(event:FlexEvent):void{
			this.toolTip = value.toString();
			var y1:Number = 600;
			var delayTime:Number = 0;
			var startParams:Object = {y:-80,x:Math.random()*500};
			var rotationValue:Number = SystemUtils.randRange(-120,120);
			var endParams:Object = {y:y1,rotation:rotationValue,delay:delayTime,time:duration,onComplete:function(){this.remove();}};
			EffectUtils.playTween(this,startParams,endParams);
			
			this.addEventListener(MouseEvent.CLICK,clickedHandler);
			//当前的值
			this.dispatchEvent(new RequestEvent(CURRENT_MONEY,value));
		}
		
		private function remove():void{
			this.dispatchEvent(new RequestEvent(REMOVE_MONEY,this));
		}
		
		private function clickedHandler(event:MouseEvent):void{
			//捡过的就不能再捡了
			if(allowedClick && clicked == false){
				clicked = true;
				//捡到后就消失
				EffectUtils.playTween(this,{alpha:1},{alpha:0,time:1,onComplete:function(){this.remove();}});
				this.dispatchEvent(new RequestEvent(CLICKED_MONEY,value));
			}
		}
	}
}